//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ShippingSettingsControl : BaseNopAdministrationUserControl
    {
        protected override void OnInit(EventArgs e) {
            cbFreeShippingOverX.CheckedChanged+=new EventHandler(cbFreeShippingOverX_CheckedChanged);

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (!IsPostBack)
                BindData();
        }

        void cbFreeShippingOverX_CheckedChanged(object sender, EventArgs e) {
            pnlValueOfX.Visible = cbFreeShippingOverX.Checked;
        }

        protected void FillCountryDropDowns() {
            this.ddlShippingOriginCountry.Items.Clear();
            ListItem selectCountryItem = new ListItem(GetLocaleResourceString("Admin.ShippingSettings.OriginCountry.SelectCountry"), "0");
            this.ddlShippingOriginCountry.Items.Add(selectCountryItem);
            var countryCollection = this.CountryService.GetAllCountries();
            foreach (Country country in countryCollection) {
                ListItem ddlCountryItem2 = new ListItem(country.Name, country.CountryId.ToString());
                this.ddlShippingOriginCountry.Items.Add(ddlCountryItem2);
            }
        }

        protected void FillStateProvinceDropDowns() {
            this.ddlShippingOriginStateProvince.Items.Clear();
            int countryId = int.Parse(this.ddlShippingOriginCountry.SelectedItem.Value);

            var stateProvinceCollection = this.StateProvinceService.GetStateProvincesByCountryId(countryId);
            foreach (StateProvince stateProvince in stateProvinceCollection) {
                ListItem ddlStateProviceItem2 = new ListItem(stateProvince.Name, stateProvince.StateProvinceId.ToString());
                this.ddlShippingOriginStateProvince.Items.Add(ddlStateProviceItem2);
            }
            if (stateProvinceCollection.Count == 0) {
                ListItem ddlStateProvinceItem = new ListItem(GetLocaleResourceString("Admin.Common.State.Other"), "0");
                this.ddlShippingOriginStateProvince.Items.Add(ddlStateProvinceItem);
            }
        }

        private void BindData() {
            cbEstimateShippingEnabled.Checked = this.SettingManager.GetSettingValueBoolean("Shipping.EstimateShipping.Enabled");
            cbFreeShippingOverX.Checked = this.SettingManager.GetSettingValueBoolean("Shipping.FreeShippingOverX.Enabled");
            pnlValueOfX.Visible = cbFreeShippingOverX.Checked;
            txtFreeShippingOverX.Value = this.SettingManager.GetSettingValueDecimalNative("Shipping.FreeShippingOverX.Value");

            Address shippingOriginAddress = this.ShippingService.ShippingOrigin;
            this.FillCountryDropDowns();
            CommonHelper.SelectListItem(this.ddlShippingOriginCountry, shippingOriginAddress.CountryId);
            this.FillStateProvinceDropDowns();
            CommonHelper.SelectListItem(this.ddlShippingOriginStateProvince, shippingOriginAddress.StateProvinceId);
            txtShippingOriginZipPostalCode.Text = shippingOriginAddress.ZipPostalCode;
        }

        protected void ddlShippingOriginCountry_SelectedIndexChanged(object sender, EventArgs e) {
            FillStateProvinceDropDowns();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            if (Page.IsValid) {
                try {
                    this.SettingManager.SetParam("Shipping.EstimateShipping.Enabled", cbEstimateShippingEnabled.Checked.ToString());
                    this.SettingManager.SetParam("Shipping.FreeShippingOverX.Enabled", cbFreeShippingOverX.Checked.ToString());
                    this.SettingManager.SetParamNative("Shipping.FreeShippingOverX.Value", txtFreeShippingOverX.Value);

                    Address shippingOriginAddress = new Address();
                    shippingOriginAddress.CountryId = int.Parse(this.ddlShippingOriginCountry.SelectedItem.Value);
                    shippingOriginAddress.StateProvinceId = int.Parse(this.ddlShippingOriginStateProvince.SelectedItem.Value);
                    shippingOriginAddress.ZipPostalCode = txtShippingOriginZipPostalCode.Text;
                    this.ShippingService.ShippingOrigin = shippingOriginAddress;

                    this.CustomerActivityService.InsertActivity(
                        "EditShippingSettings",
                        GetLocaleResourceString("ActivityLog.EditShippingSettings"));

                    Response.Redirect("ShippingSettings.aspx");
                } catch (Exception exc) {
                    ProcessException(exc);
                }
            }
        }

    }
}